package com.fiveflavors.suittar.gui.inputpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import twitter4j.User;

import com.fiveflavors.suittar.Suittar;
import com.fiveflavors.suittar.SuittarCommand;

public class SuittarInputDirectMessagePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6528619932548002465L;
	JComboBox combo = null;
	SuittarTextArea textArea = null;
	JButton btnSend = null;

	JLabel lblRecipient = null;
	JLabel lbl = null;

	List<User> myFriendList = null;

	public SuittarInputDirectMessagePanel() {
        
		setBackground(new Color(154,228,232));
        setPreferredSize(new Dimension(500,80));
        setMinimumSize(new Dimension(500,80));
        setMaximumSize(new Dimension(500,80));

	    GridBagLayout layout = new GridBagLayout();
	    setLayout(layout);
	    
        GridBagConstraints gbc = new GridBagConstraints();
        
        //add Label        
        lblRecipient = new JLabel("send to :");
        lblRecipient.setFont(new Font("SansSerif",  Font.PLAIN,10));
        lblRecipient.setForeground(new Color(0,102,204));
        lblRecipient.setHorizontalAlignment(SwingConstants.CENTER);
        lblRecipient.setPreferredSize(new Dimension(50,30));
        lblRecipient.setMinimumSize(new Dimension(50,30));
        lblRecipient.setMaximumSize(new Dimension(50,30));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.weightx = 0;
        gbc.weighty = 100.0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.NORTHWEST;
        layout.setConstraints(lblRecipient, gbc);
        add(lblRecipient);

        //add JComboBox
        combo = new JComboBox();
        combo.setBackground(new Color(154,228,232));
		combo.setPreferredSize(new Dimension(330,30));
		combo.setMinimumSize(new Dimension(330,30));
        gbc.gridx = 1;
        gbc.weightx = 100.0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        layout.setConstraints(combo, gbc);
        add(combo);

        //add JTextArea
		textArea = new SuittarTextArea();
		textArea.setLineWrap(true);

		JScrollPane jsp = new JScrollPane();
		jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		jsp.setPreferredSize(new Dimension(380,50));
		jsp.setMinimumSize(new Dimension(380,50));
		jsp.setMaximumSize(new Dimension(380,50));
        jsp.getViewport().setView(textArea);
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(3,3,3,3);
        layout.setConstraints(jsp, gbc);
        add(jsp);
        
        btnSend = new JButton("Send");
        btnSend.setBackground(new Color(154,228,232));

        lbl = new JLabel("send Direct Message");
        lbl.setFont(new Font("SansSerif",  Font.PLAIN,10));
        lbl.setForeground(new Color(0,102,204));
        lbl.setHorizontalAlignment(SwingConstants.CENTER);

        JPanel pnl = new JPanel();
        pnl.setLayout(new GridLayout(2,1));
        pnl.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        pnl.add(lbl);
        pnl.add(btnSend);

        pnl.setBackground(new Color(154,228,232));
        pnl.setPreferredSize(new Dimension(120,50));
        pnl.setMinimumSize(new Dimension(120,50));
        pnl.setMaximumSize(new Dimension(120,50));

        gbc.gridy = 0;
        gbc.gridx = 2;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        gbc.weightx = 0;
        gbc.weighty = 100.0;
        gbc.insets = new Insets(0,0,0,0);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.SOUTHEAST;
        layout.setConstraints(pnl, gbc);
        add(pnl);

	}
	
	public void addButtonActionListener(Suittar suittar) {
		btnSend.addActionListener(new SuittarStatusUpdateActionListener(suittar,SuittarCommand.COMMAND_SEND_DIRECT_MESSAGE));
	}
	
	public void addEnterActionListener(Suittar suittar) {
		textArea.getDocument().addDocumentListener(new SuittarStatusUpdateActionListener(suittar,SuittarCommand.COMMAND_SEND_DIRECT_MESSAGE));
	}
	
	public void setFriendList(List<User> myFriendList) {
		this.myFriendList = myFriendList;
		combo.removeAllItems();
		for(User user:myFriendList) {
			combo.addItem(user.getScreenName());
		}
	}

	public String getText() {
		return textArea.getText();
	}
	
	public String getRecipientId() {
		int selectedIdx = combo.getSelectedIndex();
		User user = myFriendList.get(selectedIdx);
		return String.valueOf(user.getId());
	}

	public void clear() {
		textArea.setText("");
	}
	
	public void setTextAreaEditable(boolean editable) {
		textArea.setEditable(editable);
	}

	
}
